package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.crm.entity.GwdiT;
import cn.e4j.modular.crm.service.impl.GwdiTServiceImpl;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 我的消息
 * Created by mengyb on 2019-08-30.
 */
@Controller
@RequestMapping("/crm/gwai880")
public class Gwai880Controller extends BaseController {

    @Autowired
    private GwdiTServiceImpl gwdiTService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    /**
     * 跳转客户等级分类管理页面
     */
    @RequiresPermissions("crm:gwai880:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai880.html";
    }


    /**
     * 查询销售渠道信息列表
     */
    @RequestMapping("/getGwdiList")
//    @RequiresPermissions("crm:gwai880:init")
    @ResponseBody
    public ResponseData getGwdiList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser user = ShiroKit.getUser();
        List<Dto> gwam = gwdiTService.getGwamList(inDto);
        if(gwam.size() > 0){
            for(Dto gwamDto : gwam){
                GwdiT gwdiT = new GwdiT();
                gwdiT.setGWDIENT(user.getDBC());
                //int gwdi001 = gwdiTService.getMaxGwal002(inDto);
                //gwdiT.setGWDI001(new BigDecimal(gwdi001));
                gwdiT.setGWDI002("产品资料维护");
                gwdiT.setGWDI003(gwamDto.getString("GWAM001"));
                gwdiT.setGWDI004(LocalDate.now());
                gwdiT.setGWDI005("产品编号:" + gwamDto.getString("GWAM001") + "无效，请审核!");
                gwdiT.setGWDI006("N");
                gwdiT.setGWDI007("N");
                gwdiT.setGWDIACTI("Y");
                gwdiT.insert();
            }
        }
        List<Map<String, Object>> list = sysRoleMapper.listRolesOfUser(user.getID());
        List<Dto> role = new ArrayList<>();
        for(Map<String, Object> item : list){
            Dto dto = Dtos.newDto();
            if(item.get("NAME").toString().equals("财务部")){
                dto.put("role", "渠道价格");
                role.add(dto);
                Dto pDto = Dtos.newDto();
                pDto.put("role", "组合促销活动");
                role.add(pDto);
                Dto pDto1 = Dtos.newDto();
                pDto1.put("role", "非组合促销活动");
                role.add(pDto1);
                Dto pDto2 = Dtos.newDto();
                pDto2.put("role", "合同协议");
                role.add(pDto2);
                Dto pDto3 = Dtos.newDto();
                pDto3.put("role", "订单变更");
                role.add(pDto3);
            } else if(item.get("NAME").toString().equals("技术部")){
                dto.put("role", "产品资料维护");
                role.add(dto);
            }else if(item.get("NAME").toString().equals("销售部助理")){
                dto.put("role", "注册信息维护");
                role.add(dto);
            } else if(item.get("NAME").toString().equals("销售总监")){
                dto.put("role", "客户资料维护");
                role.add(dto);
            }
        }
        List<Map<String, Object>> outDto = new ArrayList<>();
        if(role.size() > 0){
            inDto.put("role", role);
            outDto = gwdiTService.getGwdiList(page,inDto);
        }
        return new PageReData(page, outDto);
    }

    @RequestMapping("/getDetailData")
    @ResponseBody
    public ResponseData getDetailData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwdiTService.getDetailData(inDto.getString("GWDI001"));
        return ResponseData.success(result);
    }

    /**
     * 修改消息状态
     */
    @RequestMapping("/updateStatus")
//    @RequiresPermissions("crm:gwai880:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();;
        gwdiTService.updateStatus(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 修改消息状态
     */
    @RequestMapping("/update")
//    @RequiresPermissions("crm:gwai880:init")
    @ResponseBody
    public ResponseData update(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();;
        gwdiTService.update(inDto);
        return SUCCESS_TIP;
    }
}
